using MediatR;
using Microsoft.AspNetCore.Mvc;
using Medical.ErrorCode;
using BaseData.ErrorCode;
using Medical.ReadApi.Dto;
using Medical.WriteApi.Appcontext.Command;
using Medical.WriteApi.Dto;

namespace Medical.WriteApi.Controllers
{
	/// <summary>
	/// 住院单管理控制器（写操作）
	/// </summary>
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class InpatientManagementController : ControllerBase
	{
		private readonly IMediator _mediator;
		
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="mediator">MediatR中介者</param>
		public InpatientManagementController(IMediator mediator)
		{
			_mediator = mediator;
		}

		/// <summary>
		/// 创建住院单模板
		/// </summary>
		/// <param name="command">创建住院单模板命令，包含模板配置信息</param>
		/// <returns>创建成功的模板信息</returns>
		/// <remarks>
		/// 示例请求:
		/// POST /api/InpatientManagement/Create
		/// {
		///   "templateName": "住院单",
		///   "description": "默认住院单模板",
		///   "isActive": true,
		///   "isDefault": false,
		///   "showSerialNumber": true,
		///   "showDepartment": true,
		///   "showDoctor": true,
		///   "showBedNumber": true,
		///   "showAdmissionTime": true,
		///   "showDischargeTime": true,
		///   "showExpenseDetails": true,
		///   "showCashier": true,
		///   "showAmount": true
		/// }
		/// </remarks>
		/// <response code="200">创建成功，返回模板信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="500">服务器内部错误</response>
		[ProducesResponseType(typeof(ApiResult<InpatientOrderTemplateDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(500)]
		[HttpPost]
		public async Task<ApiResult<InpatientOrderTemplateDto>> Create([FromBody] CreateInpatientOrderTemplateCommand command)
		{
			try
			{
				var result = await _mediator.Send(command);
				return new ApiResult<InpatientOrderTemplateDto> { Code = ApiEnum.新增成功, Message = "创建成功", data = result };
			}
			catch (Exception ex)
			{
				return new ApiResult<InpatientOrderTemplateDto> { Code = ApiEnum.新增失败, Message = ex.Message, data = null };
			}
		}

		/// <summary>
		/// 更新住院单模板
		/// </summary>
		/// <param name="command">更新住院单模板命令，包含模板ID和要更新的字段</param>
		/// <returns>更新后的模板信息</returns>
		/// <remarks>
		/// 示例请求:
		/// PUT /api/InpatientManagement/Update
		/// {
		///   "templateId": 1,
		///   "templateName": "新模板名",
		///   "description": "新描述",
		///   "isActive": true
		/// }
		/// </remarks>
		/// <response code="200">更新成功，返回更新后的模板信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">模板不存在</response>
		/// <response code="500">服务器内部错误</response>
		[ProducesResponseType(typeof(ApiResult<InpatientOrderTemplateDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		[HttpPut]
		public async Task<ApiResult<InpatientOrderTemplateDto>> Update([FromBody] UpdateInpatientOrderTemplateCommand command)
		{
			try
			{
				var result = await _mediator.Send(command);
				return new ApiResult<InpatientOrderTemplateDto> { Code = ApiEnum.更新成功, Message = "更新成功", data = result };
			}
			catch (Exception ex)
			{
				return new ApiResult<InpatientOrderTemplateDto> { Code = ApiEnum.更新失败, Message = ex.Message, data = null };
			}
		}

		/// <summary>
		/// 删除住院单模板（逻辑删除）
		/// </summary>
		/// <param name="templateId">模板ID</param>
		/// <param name="deletedBy">删除操作人（可选）</param>
		/// <returns>删除操作是否成功</returns>
		/// <remarks>
		/// 示例请求:
		/// DELETE /api/InpatientManagement/Delete?templateId=1&amp;deletedBy=admin
		/// 
		/// 注意：此操作是逻辑删除，不会真正删除数据库中的记录。
		/// </remarks>
		/// <response code="200">删除成功</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">模板不存在</response>
		/// <response code="500">服务器内部错误</response>
		[ProducesResponseType(typeof(ApiResult<bool>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		[HttpDelete]
		public async Task<ApiResult<bool>> Delete([FromQuery] long templateId, [FromQuery] string? deletedBy = null)
		{
			try
			{
				var cmd = new DeleteInpatientOrderTemplateCommand { TemplateId = templateId, DeletedBy = deletedBy };
				var ok = await _mediator.Send(cmd);
				return new ApiResult<bool> { Code = ApiEnum.删除成功, Message = "删除成功", data = ok };
			}
			catch (Exception ex)
			{
				return new ApiResult<bool> { Code = ApiEnum.删除失败, Message = ex.Message, data = false };
			}
		}
	}
}
